/**
 * 获取Grid的json数据
 * @param vRecords
 * @returns
 */
var convertJson = function(vRecords){
	var vCount = vRecords.length;
	var vDatas = "";
	if (vCount > 0) {
		vDatas = '[';
		for (var i = 0; i < vCount; i++) {
			vDatas += JSON.stringify(vRecords[i].data)+ ',';
		}
		vDatas = vDatas.substr(0, vDatas.length - 1) + ']';
	}
	
	return vDatas;
};
/**
 * 格式化Date数据
 * @param value
 * @returns
 */
var dateFormat = function(value,format){ 
    if(null != value){ 
        return Ext.Date.format(new Date(value),format); 
    }else{ 
        return null; 
    } 
}; 

/**
 * 显示成功提示信息
 * 
 * @param {}
 *            msg
 */
var showSuccess = function(msg) {
	Ext.MessageBox.show({
        width:150,
        title:'成功',
        buttons: Ext.MessageBox.OK,
        msg:msg
    });
};

/**
 * 显示错误提示信息
 * 
 * @param {}
 *            msg
 */
var showError = function(msg) {
	if(msg == 'timeout'){
		window.location.href='logout.jsp?timeout=true';
	}else{
		Ext.MessageBox.show({
	        width:150,
	        title:'失败',
	        buttons: Ext.MessageBox.OK,
	        msg:msg
	    });
	}
	
};

/**
 * 显示警告提示信息
 * 
 * @param {}
 *            msg
 */
var showWarning = function(msg) {
	Ext.MessageBox.show({
        width:150,
        title:'警告',
        buttons: Ext.MessageBox.OK,
        msg:msg
    });
};

/**
 * 重写TreeStore的load方法
 */
Ext.override(Ext.data.TreeStore, {
    load : function(options) {
        options = options || {};
        options.params = options.params || {};

        var me = this, node = options.node || me.tree.getRootNode(), root;

        // If there is not a node it means the user hasnt defined a rootnode
        // yet. In this case lets just
        // create one for them.
        if (!node) {
            node = me.setRootNode( {
                expanded : true
            });
        }

        if (me.clearOnLoad) {
            node.removeAll(false);
        }

        Ext.applyIf(options, {
            node : node
        });
        options.params[me.nodeParam] = node ? node.getId() : 'root';

        if (node) {
            node.set('loading', true);
        }
        return me.callParent( [ options ]);
    }
});

/**
 *****************下面是权限相关的方法********************************************************************
 */

/**
 * 获取子模块
 * @returns
 */
var getChildModules = function(moduleURL){
	var parent = getIdByURL(moduleURL);
	var data = [];
    for(var i=0; i < resourceArray.length; i++){
    	var recource = resourceArray[i];
    	if(recource['parent'] == parent){
    		data.push({
            	title: recource['name'],
            	header:false,
            	border:false,
            	items : [ Ext.create(recource['url'])]
            });
    	}
    }
    
    return data;
};

/**
 * 获取顶级模块(desktop module)
 * @returns
 */
var getTopModules = function(){
	var data = [];
	for(var i=0; i < resourceArray.length; i++){
		var recource = resourceArray[i];
		if(recource['parent'] == 0){
			data.push({
				name : recource['name'],
				iconCls : recource['resourceIcon'],
				module : recource['url']
			});
		}
	}
	
	return data;
};


/**
 * 获取模块上的按钮
 * @param moduleURL
 * @returns
 */
var getButtons = function(moduleURL) {
	var buttons = [];
	var parent = getIdByURL(moduleURL);
	
	for ( var i = 0; i < resourceArray.length; i++) {
		var recource = resourceArray[i];
		if(recource['parent'] == parent){
			buttons.push(recource['url']);
		}
	}
	
	return buttons.join(',');
};

/**
 * 获取模块ID
 * @param moduleURL
 * @returns
 */
var getIdByURL = function(moduleURL){
	var moduleId = null;
	for ( var i = 0; i < resourceArray.length; i++) {
		var recource = resourceArray[i];
		if (recource['url'] == moduleURL) {
			moduleId = recource['id'];
		}
	}
	
	return moduleId;
};
